import com.alibaba.fastjson.JSON;
import com.jiugang.cardump.pojo.CarDumperPOJO;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


@SpringBootTest
public class SimulationInsert2 {

    @Test
    public void test() {

        //获取Http客户端(可以理解为先有一个浏览器,注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建Post请求
        HttpPost httpPost = new HttpPost("http://localhost:9001/monitor");

        //相应模型
        CloseableHttpResponse response = null;
        // 由客户端执行(发送)Post请求
        try {

            while (true) {
                TimeUnit.SECONDS.sleep(1);
                for (int i = 0; i < 2; i++) {
                    //String ID ="A"+((int)Math.floor(Math.random() * (5 - 1)) + 1);  //设备Id A1 A2 A3 A4
                    //String ID = "A"+((int) Math.floor(Math.random() * (4 - 1)) + 1);
                    String ID = "1";
                    String DGZT = "" + ((int) Math.floor(Math.random() * (3 - 0)) + 0);
                    String YCZT = "" + ((int) Math.floor(Math.random() * (4 - 0)) + 0);
                    String GGZT = "" + ((int) Math.floor(Math.random() * (3 - 0)) + 0);
                    String DGJGZT = "" + ((int) Math.floor(Math.random() * (2 - 0)) + 0);
                    String AIXBXH = "" + ((int) Math.floor(Math.random() * (4 - 0)) + 0);

                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    String now = dtf.format(LocalDateTime.now());

                    CarDumperPOJO carDumperPOJO = new CarDumperPOJO();
                    carDumperPOJO.setID(ID).
                            setDGZT(DGZT).setYCZT(YCZT).
                            setGGZT(GGZT).setDGJGXH(DGJGZT).setYCBXH1(DGJGZT).setYCBXH2(DGJGZT)
                            .setYCBXH3(DGJGZT)
                            .setYCBXH4(DGJGZT)
                            .setAIXBXH(AIXBXH)
                            .setFZJD("30").setSJSJ(now);

                    String jsonString = JSON.toJSONString(carDumperPOJO);

                    StringEntity entity = new StringEntity(jsonString, "UTF-8");

                    // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
                    httpPost.setEntity(entity);

                    httpPost.setHeader("Content-Type", "application/json;charset=utf8");

                    response = httpClient.execute(httpPost);
                    HttpEntity responseEntity = response.getEntity();
                    // 从响应模型中获取响应实体
                    if (responseEntity != null) {
                        System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                        System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }











}
